package com.zmx.patients.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.wang.adapters.adapter.BaseAdapterRvList;
import com.wang.adapters.base.BaseViewHolder;
import com.zmx.patients.R;
import com.zmx.patients.pojo.Patient;
import com.zmx.patients.utils.AllUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AllPatientApadter extends BaseAdapterRvList<AllPatientApadter.ViewHolder, Patient> {

    private int mLastCheckedPosition = -1;

    public AllPatientApadter(Activity activity) {
        super(activity);
    }

    @Override
    protected void onBindVH(ViewHolder holder, int listPosition, Patient patient) {

        if (mLastCheckedPosition != -1 && mLastCheckedPosition == listPosition){
            holder.relayout.setBackgroundResource(R.color.there);
        }else {
            holder.relayout.setBackgroundResource(R.color.hui);
        }

        SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");

        String gx = "";
        if(patient.getRelation() == 1){
            gx = "父母";
        }else if(patient.getRelation() == 2){
            gx = "妻子";
        }else if(patient.getRelation() == 3){
            gx = "儿女";
        }else if(patient.getRelation() == 4){
            gx = "本人";
        }else{
            gx = "其他";
        }

        holder.name.setText(patient.getPatient_name()+"("+gx+")");

        String sex = "";
        if (patient.getSex().equals("1")) {
            sex = "男";
        } else {
            sex = "女";
        }

        try {
            holder.message.setText(sex + "," + AllUtil.getAgeByBirth(s.parse(patient.getBirthday())) + "岁," + patient.getWeight() + "kg");
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    @NonNull
    @Override
    protected ViewHolder onCreateVH(ViewGroup parent, LayoutInflater inflater) {
        AllPatientApadter.ViewHolder holder = new AllPatientApadter.ViewHolder(inflater.inflate(R.layout.adapter_allpatient, parent, false));
        return holder;
    }

    public void setItemChecked(int position) {

        mLastCheckedPosition = position;
        notifyDataSetChanged();
    }

    static class ViewHolder extends BaseViewHolder {

        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.message)
        TextView message;
        @BindView(R.id.relayout)
        RelativeLayout relayout;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
